import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入子组件
import Login from '@/components/login'
import Welcome from '@/components/welcome'

import Users from '@/components/user/users'

import Roles from '@/components/power/roles'
import Rights from '@/components/power/rights'

import Goods from '@/components/good/goods'
import Params from '@/components/good/params'
import Categories from '@/components/good/categories'
import Add from '@/components/good/add'

import Orders from '@/components/order/orders'

import Reports from '@/components/report/reports'

// 导入请求时显示加载效果插件
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)
// 配置路由规则
/* eslint-disable */
// eslint-disable-next-line
const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/welcome',
    component: Welcome,
    children: [{
        path: '/users',
        component: Users
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/goods',
        component: Goods
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/orders',
        component: Orders
      },
      {
        path: '/reports',
        component: Reports
      },
      {
        path: '/goods/add',
        component: Add
      }
    ]
  }
]
// 创建路由实例并配置路由规则
const router = new VueRouter({
  routes,
  mode: 'hash'
})

// 路由发送请求之前拦截
router.beforeEach((to, from, next) => {
  var token = window.sessionStorage.getItem('token')
  if (to.path !== '/login') {
    if (token) {
      next()
    } else {
      next({
        path: '/login'
      })
    }
  }
  NProgress.start()
  next()
})

export default router
